package com.lanlengran.lancher;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanlengran.lancher.utils.AppUtils;
import com.lanlengran.lancher.utils.NetUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView batteryTv;
    private RecyclerView recyclerView;
    private ImageView batteryIcon;
    private ImageView netState;
    private BatteryReceiver batteryReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBattery();
        initAppList();
        initTitleBar();
    }

    private void initTitleBar() {
        findViewById(R.id.title_bar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppUtils.startAPP("com.android.settings");
            }
        });
    }

    private void initAppList() {
        recyclerView= (RecyclerView) findViewById(R.id.recylerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(this,Config.getPageItemNum()));
        recyclerView.setAdapter(new AppItemAdapter(this, AppUtils.loadApps()));
    }

    protected void initBattery() {
        batteryTv = (TextView) findViewById(R.id.battery_num_textview);
        batteryIcon = (ImageView) findViewById(R.id.battery_state_imageview);
        netState = (ImageView) findViewById(R.id.net_state_imageview);
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        batteryReceiver=new BatteryReceiver();
        registerReceiver(batteryReceiver, filter);

        netState.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
    }

    class BatteryReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            //判断它是否是为电量变化的Broadcast Action
            if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
                //获取当前电量
                int level = intent.getIntExtra("level", 0);
                //电量的总刻度
                int scale = intent.getIntExtra("scale", 100);
                int nowPower = (level * 100) / scale;
                //把它转成百分比
                batteryTv.setText(nowPower + "%");
                int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
                if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
                    batteryIcon.setImageResource(R.drawable.icon_battery_online);
                } else {
                    if (nowPower > 90) {
                        batteryIcon.setImageResource(R.drawable.icon_battery_100);
                    } else if (nowPower > 70) {
                        batteryIcon.setImageResource(R.drawable.icon_battery_80);
                    } else if (nowPower > 50) {
                        batteryIcon.setImageResource(R.drawable.icon_battery_60);
                    } else if (nowPower > 30) {
                        batteryIcon.setImageResource(R.drawable.icon_battery_40);
                    } else if (nowPower > 10) {
                        batteryIcon.setImageResource(R.drawable.icon_battery_20);
                    } else {
                        batteryIcon.setImageResource(R.drawable.icon_battery_0);
                    }
                }

            } else {
                if (NetUtils.getWifiState(intent)) {
                    netState.setImageResource(R.drawable.icon_net_has);
                } else {
                    netState.setImageResource(R.drawable.icon_net_null);
                }

            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(batteryReceiver);
    }
}
